package com.hopster;

import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

	
public class EventPage extends Activity 
{
	private LinearLayout elayout;
	Bundle date;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_page);
        
        date = getIntent().getExtras();
        TextView day = (TextView) findViewById(R.id.daytextview);
        elayout = (LinearLayout)findViewById(R.id.event_page_layout);
        Button back = (Button) findViewById(R.id.back);
	    Button addEvent = (Button) findViewById(R.id.addEvent);
	    
	    day.setText(Integer.toString(date.getInt("month")+1) + "/"
	    		+ Integer.toString(date.getInt("day")) + "/"
	    		+ Integer.toString(date.getInt("year"))
	    );
	    
	    addEvent.setOnClickListener(new OnClickListener() 
	    {
	    	public void onClick(View v) {
	    		Intent i = new Intent();
	    		i.setClassName("com.hopster", "com.hopster.CreateEvent");
	    		i.putExtras(date);
	    		startActivity(i);
	    	}
	    });
	    
	    back.setOnClickListener(new OnClickListener() 
	    {
	    	public void onClick(View v) {
	    		finish();
	    	}
	    });
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        
        EventService es = EventService.getInstance();
        final List<Event> events = es.getEvents();
        
        try
        {
	        while(true)
	        {
	        	elayout.removeViewAt(1);
	        }
        }
        catch(Exception e) { }
        
        for(final Event event: events)
        {
        	if((event.getStartCalendar().get(Calendar.YEAR) == date.getInt("year")) && (event.getStartCalendar().get(Calendar.MONTH) == date.getInt("month")) && (event.getStartCalendar().get(Calendar.DAY_OF_MONTH) == date.getInt("day")))
        	{
        		TextView textView = new TextView(EventPage.this);
        		final String name = event.getName();
        		final int hour = event.getStartCalendar().get(Calendar.HOUR);
        		final int minute = event.getStartCalendar().get(Calendar.MINUTE);
        		String am_pm;
        		if(event.getStartCalendar().get(Calendar.AM_PM) == 0)
        			am_pm = "AM";
        		else
        			am_pm = "PM";
        		String sMinute = (minute == 0) ? "00" : 
					  (minute < 10) ? "0" + minute
									: "" + minute;
        		textView.setText(name + " " + hour + ":" + sMinute + " " + am_pm);
        		textView.setTextSize(30f);
        		elayout.addView(textView);
        		
        		textView.setOnClickListener(new View.OnClickListener()
            	{
    				
    				public void onClick(View v) 
    				{
    					Intent in = new Intent();
    					int ev = events.indexOf(event);
                		Bundle b = event.passEvent(ev);
                        in.putExtras(b);
    	        		in.setClassName("com.hopster", "com.hopster.EditEvent");
    	        		startActivity(in);
    				}
    			});
        	}
        }
    }
}